import pageStyles from '@/common/pageLayout.less';
import { Table, message } from 'antd';
import { useMemo, useState } from 'react';
import { getHomeTableColumns } from '../mconfig';
import { getKeyRouter } from '@/utils';
import Cls from 'classnames';
import { commonPageSizeOptions, showTotalInfo } from '@/common/constVariable';
import { history } from 'umi';
import { getMcontractByIDService } from '../services';
import { ContractHomesAPI } from '../common/type';
import { usePageRouterPayload } from '@/layouts/UserLayout/MyHooks';

export const ContractHomeTableBody: React.FC<ContractHomesAPI.IContractHomeTableBody> = ({
  curRes,
  tabPaneKey,
  listTableData,
  pageSizeObj,
  getTaskCheckData,
}) => {
  /** 保存变更合同ID */
  const [modifyInfoDetail, setModifyInfoDetail] = useState<Record<string, any>>({});

  /** 详情回调 */
  const detailsCallback = (res: any) => {
    if (res && res?.msgFlag) {
      history.push({
        pathname: `/contractController/getMcontractByID/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          // id: modifyInfoDetail?.clickType ? modifyInfoDetail?.pID : modifyInfoDetail?.id,
          id: modifyInfoDetail?.id,
          pageInfo: {
            pID: modifyInfoDetail?.pID ? modifyInfoDetail?.pID : null,
            id: modifyInfoDetail?.id ? modifyInfoDetail?.id : null,
          },
          pIDDetailInfo: { ...res?.obj },
        },
      });
      setModifyInfoDetail({});
    } else {
      message.error(res?.msg);
    }
  };

  /** 获取详情 */
  const { getMcontractByIDRun } = getMcontractByIDService(detailsCallback);

  /** 进入详情办理页面方法 */
  const onViewHand = (resObj: Record<string, any>) => {
    /** 结构pcUrl中的state参数 */
    let slicePcUrl = usePageRouterPayload(resObj?.pcUrl?.split('state=')[1]);
    let pID = slicePcUrl?.pBusinessID;
    let id = slicePcUrl?.businessID;

    if (slicePcUrl?.changeWayStr === '0' || slicePcUrl?.changeWayStr === '1') {
      setModifyInfoDetail({ pID, id });
      if (pID) {
        getMcontractByIDRun({ id: pID });
      }
    } else if (slicePcUrl?.changeWayStr === 'null') {
      history.push({
        pathname: `/contractController/getMcontractByID/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          id: id,
          pageInfo: {
            pID: pID || null,
            id: id || null,
          },
          pIDDetailInfo: {
            ...resObj?.attributes,
            id: id || null,
            pID: pID || null,
            contractCode: null,
            // contractCode: resObj?.attributes?.busi_contractCode,
          },
        },
      });
    }

    /** 原代码 */
    // if (resObj?.attributes.busi_changeWay == 0 || resObj?.attributes.busi_changeWay == 1) {
    //   setModifyInfoDetail(resObj?.attributes);
    //   getMcontractByIDRun({ id: resObj?.attributes.busi_pID });
    // } else {
    //   history.push({
    //     pathname: `/contractController/getMcontractByID/:${getKeyRouter()}${getKeyRouter()}`,
    //     state: {
    //       pageType: 'detail',
    //       id: resObj?.attributes.busi_id,
    //       pageInfo: {
    //         pID: resObj?.attributes?.busi_pID ? resObj?.attributes?.busi_pID : null,
    //         id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
    //       },
    //       pIDDetailInfo: {
    //         ...resObj?.attributes,
    //         id: resObj?.attributes?.busi_id ? resObj?.attributes?.busi_id : null,
    //         pID: resObj?.attributes?.busi_pID ? resObj?.attributes?.busi_pID : null,
    //         contractCode: resObj?.attributes?.busi_contractCode,
    //       },
    //     },
    //   });
    // }
  };

  /** OA测试回调 */
  const oaDetailCallback = (res: any) => {
    if (res && res?.msgFlag) {
      history.push({
        pathname: `/contractController/taskDataGrid/{paramValue}/{params}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          id: modifyInfoDetail?.id,
          pageInfo: {
            pID: modifyInfoDetail?.pID ? modifyInfoDetail?.pID : null,
            id: modifyInfoDetail?.id ? modifyInfoDetail?.id : null,
          },
          pIDDetailInfo: { ...res?.obj },
        },
      });
      setModifyInfoDetail({});
    } else {
      message.error(res?.msg);
    }
  };

  /** OA测试 */
  const oaTest = getMcontractByIDService(oaDetailCallback);
  const onOA = (resObj) => {
    /** 结构pcUrl中的state参数 */
    let slicePcUrl = usePageRouterPayload(resObj?.pcUrl?.split('state=')[1]);
    let pID = slicePcUrl?.pBusinessID;
    let id = slicePcUrl?.businessID;

    if (slicePcUrl?.changeWayStr === '0' || slicePcUrl?.changeWayStr === '1') {
      setModifyInfoDetail({ pID, id });
      // if (pID) { getMcontractByIDRun({ id: pID }) }
      if (pID) {
        oaTest?.getMcontractByIDRun({
          id: pID,
        });
      }
    } else if (slicePcUrl?.changeWayStr === 'null') {
      history.push({
        pathname: `/contractController/taskDataGrid/{paramValue}/{params}/:${getKeyRouter()}${getKeyRouter()}`,
        state: {
          pageType: 'detail',
          id: id,
          taskId: id,
          pageInfo: {
            pID: pID || null,
            id: id || null,
          },
          pIDDetailInfo: {
            // ...resObj?.attributes,
            id: id || null,
            pID: pID || null,
            contractCode: null,
            // contractCode: resObj?.attributes?.busi_contractCode,
          },
        },
      });
    }
  };

  /** 点击方法 */
  const onOptClick: ContractHomesAPI.IOperationClick = {
    onView: (resObj) => {
      onViewHand(resObj);
    },
    onHand: (resObj) => {
      onViewHand(resObj);
    },
    onOA: (resObj) => {
      onOA(resObj);
    },
  };

  /** 返回表格配置 */
  const columnsMemo = useMemo(() => {
    return getHomeTableColumns(curRes, tabPaneKey == '1' ? true : false, onOptClick);
  }, [tabPaneKey, curRes]);

  // 分页页数 和数据条数改变
  const handlePaginationChange = (page: number, pageSize: number) => {
    let params = {
      ...pageSizeObj,
      current: page,
      pageSize: pageSize,
      taskType: tabPaneKey == '1' ? false : true,
    };
    getTaskCheckData(params);
  };

  return (
    <div className="commonTabsBodyDom" style={{ padding: '5px 0px' }}>
      <div className={Cls(pageStyles['listTable-zIndexStyle'], pageStyles['listTable-notBorder'])}>
        <Table
          size="small"
          bordered={false}
          columns={columnsMemo}
          scroll={{ x: '100vh', y: 'true' }}
          dataSource={listTableData?.rows}
          rowKey={(res: any) => res?.id || getKeyRouter()}
          pagination={{
            showQuickJumper: true,
            showSizeChanger: true,
            pageSizeOptions: commonPageSizeOptions,
            showTotal: showTotalInfo,
            onChange: handlePaginationChange,
            pageSize: pageSizeObj?.pageSize,
            current: pageSizeObj?.current,
            total: pageSizeObj?.total,
            hideOnSinglePage: true,
          }}
        />
      </div>
    </div>
  );
};
